package main

import (
	"bufio"
	"database/sql"
	"flag"
	"fmt"
	"log"
	"os"
	"sync"
	"yjcz-spiders/lib"

	_ "github.com/go-sql-driver/mysql"
)

var wg sync.WaitGroup

func main() {
	wg.Add(8)

	var mysqlConf string
	flag.StringVar(&mysqlConf, "mysqlConf", "data/mysqlconf.txt", "数据库配置文件")
	flag.Parse()

	file, err := os.Open(mysqlConf)
	if err != nil {
		log.Fatal("数据库配置文件无法加载,请先添加 data/mysqlconf.txt 文件")
	}
	reader := bufio.NewReader(file)
	dns, _, _ := reader.ReadLine()

	db, err := sql.Open("mysql", string(dns))
	defer db.Close()
	if err != nil {
		log.Fatal("数据库链接失败")
	}

	// 首页信息公开
	go collect(db, &lib.GovPub{}, "http://www.gd.gov.cn/xxts/", 1)

	// 财政预算
	go collect(db, &lib.CzSz{}, "http://www.gdczt.gov.cn/zwgk/sgjf/sjqk/", 2)
	// 税收政策
	go collect(db, &lib.SsZc{}, "http://www.gdczt.gov.cn/zwgk/sgjf/jsjf/sszc/", 3)
	// 政府性基金项目目录
	go collect(db, &lib.ZfJj{}, "http://www.gdczt.gov.cn/zwgk/sgjf/jsjf/jjml/", 4)
	// 政府社会资本合作（PPP）项目
	go collect(db, &lib.Ppp{}, "http://www.gdczt.gov.cn/zwgk/sgjf/PPP/", 5)
	// 政策解读
	go collect(db, &lib.Zcjd{}, "http://www.gdczt.gov.cn/zwgk/zcjd/", 6)

	// 习总书记讲话
	go collect(db, &lib.XjpJhXxzl{}, "http://www.12371.cn/special/xxzd/jh/", 8)
	//go collect(db, &lib.XjpJhXxzl{}, "http://www.12371.cn/special/xxzd/hd/", 8)

	// 十九大
	go collect(db, &lib.XjpJhXxzl{}, "http://www.12371.cn/special/xxzd/tg/", 7)

	wg.Wait()
}

func collect(db *sql.DB, r lib.Ruler, target string, catType int8) {
	result := lib.Parse(r, target)
	defer wg.Done()

	if len(result) == 0 {
		return
	}
	//log.Printf("result is %v", result)
	for key, value := range result["title"] {
		if value == "" {
			continue
		}

		link := ""
		date := ""

		if hasIndex(key, result["link"]) {
			link = result["link"][key]
		}

		if hasIndex(key, result["date"]) {
			date = result["date"][key]
		}

		if link == "" {
			continue
		}

		stmt, err := db.Prepare("insert into v9_import_thing(title, link, date, type) values(?,?,?,?)")
		if err != nil {
			log.Fatalf("stmt error:%s", err.Error())
		}
		selectSql := fmt.Sprintf("select count(*) from v9_import_thing where link='%s' and type=%d",
			link, catType)
		var isExist bool
		db.QueryRow(selectSql).Scan(&isExist)
		if isExist {
			continue
		}

		_, err = stmt.Exec(value, link, date, catType)
		stmt.Close()
		if err != nil {
			continue
		}
	}
}

func hasIndex(index int, s []string) bool {
	for i := range s {
		if i == index {
			return true
		}
	}

	return false
}
